/**
 * 简化的路由配置 - 用于解决模块导入问题
 */
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import type { RouteRecordRaw } from 'vue-router'

// 直接导入组件，避免动态导入问题
import Login from '@/views/Login.vue'
import MainLayout from '@/layouts/MainLayout.vue'
import Dashboard from '@/views/Dashboard.vue'
import Departments from '@/views/Departments.vue'
import Employees from '@/views/Employees.vue'
import Students from '@/views/Students.vue'
import Courses from '@/views/Courses.vue'
import Orders from '@/views/Orders.vue'
import Performance from '@/views/Performance.vue'
import Traffic from '@/views/Traffic.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: MainLayout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表盘' }
      },
      {
        path: '/departments',
        name: 'Departments',
        component: Departments,
        meta: { title: '部门管理' }
      },
      {
        path: '/employees',
        name: 'Employees',
        component: Employees,
        meta: { title: '员工管理' }
      },
      {
        path: '/students',
        name: 'Students',
        component: Students,
        meta: { title: '学员管理' }
      },
      {
        path: '/courses',
        name: 'Courses',
        component: Courses,
        meta: { title: '课程管理' }
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders,
        meta: { title: '订单管理' }
      },
      {
        path: '/performance',
        name: 'Performance',
        component: Performance,
        meta: { title: '业绩统计' }
      },
      {
        path: '/traffic',
        name: 'Traffic',
        component: Traffic,
        meta: { title: '流量统计' }
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    next('/login')
  } else if (to.path === '/login' && userStore.isAuthenticated) {
    next('/')
  } else {
    next()
  }
})

export default router
